<?php
class codesell
{
	public static function loadurl($url,$post)
	{
		$timeout=200;
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $url); 
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3"); 
		curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); 
		curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
	public static function reloadSearch()
	{
		self::loginCodeSell();
		$url="http://codesell.net/search.php?type=new";
		$reload=self::loadurl($url, "0");
		return TRUE;
		
	}
	public static function loginCodeSell()
	{
		$urlLogin="http://codesell.net/ajax/makerequests.php";
		$post="pname=loginBox&uname=freshcv2shop&password=123456&remember=true";
		$login=self::loadurl($urlLogin, $post);
		return TRUE;
	}
	public static function GetContents($string,$start,$end)
	{
		$temp=explode($start,$string);
		$Result=explode($end,$temp[1]);
		return $Result[0];
	}
	public static function balanceCodeSell()
	{
		self::loginCodeSell();
		$home="http://codesell.net/home.php";
		$loadHome=self::loadurl($home, "0");
		$start='<b id="balance">$';
		$end='</b>';
		$balance=self::GetContents($loadHome, $start, $end);
		$balance=intval($balance);
		return $balance;
	}
	public static function findCountry()
	{
		self::loginCodeSell();
		$urlCountry="http://codesell.net/search.php";
		$findCountry=self::loadurl($urlCountry, "0");
		
		if(!empty($findCountry))
		{
			$start='<select name="country" id="country" style="width: 100px;">';
			$end='</select>';
			$listCountry=self::GetContents($findCountry, $start, $end);
			return $listCountry;
		}
		else
		{
			return FALSE;
		}
		
	}
	public static function searchCodeSell($type,$country,$bin,$dob)
	{
		if($dob=="yes")
		{
			$dob="on";
		}
		else 
		{
			$dob="";
		}
		if($bin==0)
		{
			$bin="";
		}
		
		self::reloadSearch();
		$urlSearch="http://codesell.net/search.php?pageType=serchCreditCards";
		$post="holder=$type&type1=&type2=&country=$country&bin=$bin&zip=&city=&state=&bank=&dob=$dob&submit=Search";
		$search=self::loadurl($urlSearch, $post);
		if(stristr($search,'Found cards (0)')==true)
		{
			return 0; // empty result
		}
		else
		{
			$start='id="accountBal" name="accountBal">';
			$end='<table';
			$getQuatity=self::GetContents($search, $start, $end);
			$getQuatity=rtrim(trim($getQuatity));
			$search=array('Found cards (',')');
			$replace=array("","");
			$getQuatity=str_replace($search,$replace,$getQuatity);
			$getQuatity=intval($getQuatity);
			return $getQuatity;
		}
		
	}
	public static function getCheckOut($type,$country,$bin,$dob)
	{
		
		if($dob=="yes")
		{
			$dob="on";
		}
		else 
		{
			$dob="";
		}
		if($bin==0)
		{
			$bin="";
		}
		
		self::reloadSearch();
		$urlSearch="http://codesell.net/search.php?pageType=serchCreditCards";
		$post="holder=$type&type1=&type2=&country=$country&bin=$bin&zip=&city=&state=&bank=&dob=$dob&submit=Search";
		$search=self::loadurl($urlSearch, $post);
		if(stristr($search,'Found cards (0)')==true)
		{
			return 0; // empty result
		}
		else
		{
			return $search;
		}
		
	}
	public static function getCcCodeSell($quality)
	{
		self::loginCodeSell();
		$urlView="http://codesell.net/purchase.php";
		$viewCc=self::loadurl($urlView, "0");
		$arrInfo=explode("<td>",$viewCc);
		$arrReturn=array();
		for($i=1;$i<=$quality;$i++)
		{
			$infoCcFirst=$arrInfo[$i];
			$ccLine1=explode("<p>",$infoCcFirst);
			$ccLine2=$ccLine1[0];
			$ccLine=self::getccdetail($ccLine2);
			$ccNums=$ccLine['ccnum'];
			$exMonth=$ccLine['exmonth'];
			$exYear=$ccLine['exyear'];
			$ccv=$ccLine['ccv'];
			$other=$ccLine['other'];
			$arrReturn[$i]['ccnum']=$ccNums;
			$arrReturn[$i]['exmonth']=$exMonth;
			$arrReturn[$i]['exyear']=$exYear;
			$arrReturn[$i]['ccv']=$ccv;
			$arrReturn[$i]['info']=$other;
			$infoChecker=$ccLine1[1];
			$start='checker" onclick="checker(';
			$end=');" />';
			$stringChecker=self::GetContents($infoChecker, $start, $end);
			$stringChecker=str_replace("'", "", $stringChecker);
			$arrString=explode(",",$stringChecker);
			$orderId=$arrString[0];
			$productId=$arrString[1];
			$creditCard=$arrString[2];
			$status=$arrString[3];
			$post="pname=checker&action=checker&creditCard=$creditCard&status=$status&productId=$productId&orderId=$orderId";
			$urlCheck="http://codesell.net/makerequests.php";
			$check=self::loadurl($urlCheck, $post);
		}
		return $arrReturn;
		
		
	}
	
	public static function insertCc($ccNumbers1,$exM,$exY,$ccv,$other,$country,$price,$userid,$orderid)
	{
			$firstChar=substr($ccNumbers1, 0,1);
			if($firstChar==4)
			{
				$Typecc="VISA";
			}
			else if($firstChar==5)
			{
				$Typecc="MASTER";
			}
			else if($firstChar==3)
			{
				$Typecc="AMEX";
			}
			else if($firstChar==6)
			{
				$Typecc="DIS";
			}
			else 
			{
				$Typecc="unknow";
			}
			$ccNumbers=hash::hashccv($ccNumbers1, $exM, $exY, $ccv);
			$InsertCC=database::Insert('ccinfo', array(
			'TypeCC' => $Typecc,
			'CCnumber' => $ccNumbers,
			'ExMonth' => $exM,
			'ExYear' => $exY,
			'CCV2' => $ccv,
			'Others' => $other,
			'site' => 'codesell',
			'country' => $country,
			'pricesell' => $price,
			'userid' => $userid,
			'orderid' => $orderid,
			));
		
	}
	public static function checkoutCodesell($type,$country,$bin,$dob,$quatity,$userid,$orderid,$pricex)
	{
		
		$find=self::getCheckOut($type, $country, $bin, $dob);
		$start='<input type="hidden" value="';
		$end='" id="accountBal"';
		$accountBalance=self::GetContents($find, $start, $end);
		$start="name='price_0' ".'value="';
		$end='" />';
		$price=self::GetContents($find, $start, $end);
		$urlBuy="http://codesell.net/actions.php?page=buyCC";
		$post="accountBal=$accountBalance&0=true&totalItem_0=$quatity&price_0=$price&searchCharg_0=0&withDob_0=&submit=Buy";
		$buy=self::loadurl($urlBuy, $post);
		$getCc=self::getCcCodeSell($quatity);
		if(stristr($buy,'showMessage ("Product Purcahsed Successfully.", "darkgreen")')==true)
			{
				foreach($getCc as $cc)
				{
				$ccNums=$cc['ccnum'];
				$exMonth=$cc['exmonth'];
				$exYear=$cc['exyear'];
				$ccv=$cc['ccv'];
				$info=$cc['info'];
				$insert=self::insertCc($ccNums, $exMonth, $exYear, $ccv, $info, $country, $pricex, $userid, $orderid);
				}
			}
			else
			{
				return "erro";
			}
		
		
		
	}
	public static function checkCcCodeSell($ccNumbers)
	{
		self::loginCodeSell();
		$url="http://codesell.net/purchase.php";
		$searchCc=self::loadurl($url, "0");
		if(empty($searchCc))
		{
			return 2; // erro
		}
		else if($searchCc)
		{
			if(stristr($searchCc, $ccNumbers)==true)
			{
				return 1; // liv
			}
			else
			{
				return 0; //
			}
		}
		
	}
	public static function getccdetail($soure)
	{
		$search=array(
		"	","    ","-","|",":");
		$replace=array("/","/","/","/","/");
		$ccLine=str_replace($search, $replace, $soure);
		$arrCc=preg_split("/[\s]*[,|\/	| ][\s]*/",$ccLine);
		print_r($arrCc);
		if(empty($arrCc))
		{
			return 0; // erro
		}
		else
		{
			for($i=0;$i<count($arrCc);$i++)
			{
				$variable=rtrim(trim($arrCc[$i]));
				if(ctype_digit($variable)==true)
				{
					if(strlen($variable)>14)
					{
						$ccNums=$variable;
						$firstChar=substr($ccNums, 0,1);
						$ccv="0";
						$exMonth="0";
						$exYear="0";
						if($firstChar!=3)
						{
							
							$iNext=$i+1;
							$variableNext=trim(rtrim($arrCc[$iNext]));
							if(strlen($variableNext)==3)
							{
								
								$ccv=$variableNext;
								$idNextEx=$iNext+1;
								$variableNext=rtrim(trim($arrCc[$idNextEx]));
								if(strlen($variableNext)==4)
								{
									$exMonth=substr($variableNext, 0,2);
									$exYear="20".substr($variableNext, 2,2);
									
								}
								else if(strlen($variableNext)==6)
								{
									$exMonth=substr($variableNext, 0,2);
									$exYear=substr($variableNext, 2,4);
								}
								else if(strlen($variableNext)==2)
								{
									
									$exMonth=$variableNext;
									$idYear=$idNextEx+1;
									
									$exYear=rtrim(trim($arrCc[$idYear]));
									echo $idYear;
									if(strlen($exYear)==2)
									{
										$exYear="20".$exYear;
									}
									else 
									{
										$exYear=$exYear;
									}
								}
							}
							else if(strlen($variableNext)==2)
							{
								
								$exMonth=$variableNext;
								$idYear=$iNext+1;
								$exYear=rtrim(trim($arrCc[$idYear]));
								if(strlen($exYear)==2)
								{
									$exYear="20".$exYear;
								}
								else 
								{
									$exYear=$exYear;
								}
								$idCcv=$idYear+1;
								$ccv=rtrim(trim($arrCc[$idCcv]));
							}
							else if(strlen($variableNext)==4)
							{
								
								$exMonth=substr($variableNext, 0,2);
								$exYear="20".substr($variableNext, 2,2);
								$idCcv=$iNext+1;
								$ccv=rtrim(trim($arrCc[$idCcv]));
							}
							else if(strlen($variableNext)==6)
							{
								
								$exMonth=substr($variableNext, 0,2);
								$exYear=substr($variableNext, 2,4);
								$idCcv=$iNext+1;
								$ccv=rtrim(trim($arrCc[$idCcv]));
							}
						}
						else if($firstChar==3)
						{
							$iNext=$i+1;
							$variableNext1=trim(rtrim($arrCc[$iNext]));
							if(strlen($variableNext1)==4)
							{
								
								$string1=substr($variableNext1,0,2);
								$string2=substr($variableNext1,2,2);
								if(($string1>12)||($string2<11)||($string2>17))
								{
									
									$ccv=$variableNext1;
									$idNext=$iNext+1;
									$variableNext=$arrCc[$idNext];
									if(strlen($variableNext)==2)
									{
										$exMonth=$variableNext;
										$idYear=$idNext+1;
										$exYear=rtrim(trim($arrCc[$idYear]));
										if(strlen($exYear)==2)
										{
											$exYear="20".$exYear;
										}
										else 
										{
											$exYear=$exYear;
										}
									}
									else if(strlen($variableNext)==4)
									{
										$exMonth=substr($variableNext, 0,2);
										$exYear="20".substr($variableNext, 2,2);
									}
									
									else if(strlen($variableNext)==6)
									{
										$exMonth=substr($variableNext, 0,2);
										$exYear=substr($variableNext, 2,4);
									}
								}
								else
								{
									
									$idNext=$iNext+1;
									$varNext=rtrim(trim($arrCc[$idNext]));
									if(strlen($varNext)==2)
									{
										
										$ccv=$variableNext1;
										$exMonth=$varNext;
										$idYear=$idNext+1;
										$exYear=rtrim(trim($arrCc[$idYear]));
										if(strlen($exYear)==2)
										{
											$exYear="20".$exYear;
										}
									}
									else if(strlen($varNext)==4)
									{
										
										$string1=substr($varNext,0,2);
										$string2=substr($varNext,2,2);
										if(($string1>12)||($string2<11)||($string2>17))
										{
											
											$ccv=$varNext;
											$exVar=$variableNext1;
											$exMonth=substr($exVar, 0,2);
											$exYear="20".substr($exVar, 2,2);
										}
										
									}
									else if(strlen($varNext)==6)
									{
										$exMonth=substr($varNext, 0,2);
										$exYear=substr($varNext,2,4);
										$ccv=$variableNext1;
									}
								}
							}
							else if(strlen($variableNext1)==2)
							{
								
								$exMonth=$variableNext1;
								$idYear=$iNext+1;
								$exYear=rtrim(trim($arrCc[$idYear]));
								if(strlen($exYear)==2)
								{
									$exYear="20".$exYear;
								}
								$idCcv=$idYear+1;
								$ccv=rtrim(trim($arrCc[$idCcv]));
							}
							else if(strlen($variableNext1)==6)
							{
								
								$exMonth=substr($variableNext1, 0,2);
								$exYear=substr($variableNext1, 2,4);
								$idCcv=$iNext+1;
								$ccv=rtrim(trim($arrCc[$idCcv]));
							}
							
						}
						
						
					}
					
				}
			}
			
			$search=array($ccNums,$exMonth,$exYear,$ccv);
			$replace=array("","","","");
			$other=str_replace($search, $replace, $ccLine);
			$arrReturn['ccnum']=$ccNums;
			$arrReturn['exmonth']=$exMonth;
			$arrReturn['exyear']=$exYear;
			$arrReturn['ccv']=$ccv;
			$arrReturn['other']=$other;
			return $arrReturn;
		}
		
	}
}

?>